<!-- 员工查看订单页面 clientview.php-->
<!DOCTYPE html>
<html>
<?php
session_start();
require 'connection.php';
$conn = Connect();
?>

<head>
    <link rel="shortcut icon" type="image/png" href="assets/img/P.png.png">
    <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="assets/w3css/w3.css">
    <link rel="stylesheet" type="text/css" href="assets/css/customerlogin.css">
    <script type="text/javascript" src="assets/js/jquery.min.js"></script>
    <script type="text/javascript" src="assets/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" media="screen" href="assets/css/clientpage.css" />
</head>

<body id="page-top" data-spy="scroll" data-target=".navbar-fixed-top">
    <?php include './nav.php'; ?>

    <?php
    $login_client = $_SESSION['login_client'];

    // 分页参数
    $limit = 3; // 每页显示的记录数量
    $page = isset($_GET['page']) ? $_GET['page'] : 1; // 当前页码
    $offset = ($page - 1) * $limit; // 计算偏移量

    // 查询已租赁车辆信息并进行分页
    $sql1 = "SELECT * FROM rentedcars rc, clientcars cc, customers c, cars WHERE cc.client_username = '$login_client' AND cc.car_id = rc.car_id AND 
    rc.return_status = 'R' AND c.customer_username = rc.customer_username AND cc.car_id = cars.car_id LIMIT $limit OFFSET $offset";
    $result1 = $conn->query($sql1);

    // 查询总记录数以计算总页数
    $sql_total = "SELECT COUNT(*) as total FROM rentedcars rc, clientcars cc, customers c, cars WHERE cc.client_username = '$login_client' AND cc.car_id = rc.car_id AND rc.return_status = 'R' AND c.customer_username = rc.customer_username AND cc.car_id = cars.car_id";
    $result_total = $conn->query($sql_total);
    $row_total = $result_total->fetch_assoc();
    $total_records = $row_total['total'];
    $total_pages = ceil($total_records / $limit);
    ?>

    <div class="container">
        <div class="jumbotron">
            <h1 class="text-center">您的预订</h1>
            <p class="text-center"> 希望您享受我们的服务 </p>
        </div>
    </div>

    <div class="table-responsive" style="padding-left: 100px; padding-right: 100px;">
        <table class="table table-striped">
            <thead class="thead-dark">
                <tr>
                    <th width="20%">汽车</th>
                    <th width="15%">客户姓名</th>
                    <th width="20%">租赁开始日期</th>
                    <th width="20%">租赁结束日期</th>
                    <th width="10%">里程</th>
                    <th width="15%">总金额</th>
                </tr>
            </thead>
            <?php
            while ($row = mysqli_fetch_assoc($result1)) {
            ?>
                <tr>
                    <td><?php echo $row["car_name"]; ?></td>
                    <td><?php echo $row["customer_name"]; ?></td>
                    <td><?php echo $row["rent_start_date"] ?></td>
                    <td><?php echo $row["rent_end_date"]; ?></td>
                    <td><?php echo $row["distance"]; ?></td>
                    <td>Rs. <?php echo $row["total_amount"]; ?></td>
                </tr>
            <?php } ?>
        </table>
    </div>

    <!-- 分页导航 -->
    <div class="container">
        <div class="text-center">
            <nav aria-label="Page navigation">
                <ul class="pagination justify-content-center">
                    <?php
                    $prev_page = ($page > 1) ? $page - 1 : 1;
                    $prev_disabled = ($page == 1) ? 'disabled' : '';
                    echo "<li class='page-item $prev_disabled'><a class='page-link' href='clientview.php?page=$prev_page'>上一页</a></li>";

                    for ($i = 1; $i <= $total_pages; $i++) {
                        $activeClass = ($i == $page) ? 'active' : '';
                        echo "<li class='page-item $activeClass'><a class='page-link' href='clientview.php?page=$i'>$i</a></li>";
                    }

                    $next_page = ($page < $total_pages) ? $page + 1 : $total_pages;
                    $next_disabled = ($page == $total_pages) ? 'disabled' : '';
                    echo "<li class='page-item $next_disabled'><a class='page-link' href='clientview.php?page=$next_page'>下一页</a></li>";
                    ?>
                </ul>
            </nav>
        </div>
    </div>


</body>
<footer class="site-footer">
    <div class="container">
        <hr>
        <div class="row">
            <div class="col-sm-6">
                <h5>© <?php echo date("Y"); ?> 租车服务</h5>
            </div>
        </div>
    </div>
</footer>

</html>